package com.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Reward;
import com.demo.entity.Sperson;
import com.demo.mapper.RewardMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 科研奖励表
 */
@RestController
@RequestMapping("/reward")
public class RewardController {
  /**
   * 服务对象
   */
  @Resource
  RewardMapper rewardMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Reward reward) {
    try {
      rewardMapper.insert(reward);
      return Result.success();
    } catch (Exception e) {
      System.out.println(e);
      return Result.error("201", "");
    }
    
  }
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    rewardMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody Reward reward) {
    try {
      rewardMapper.updateById(reward);
      return Result.success();
    } catch (Exception e) {
      return Result.error("201", "");
    }
    
  }
  
  /**
   * 分页,默认1页,10个数据,  默认根据 项目名称Itemname  查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @param sort     搜索类别
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "Itemname") String sort) {
    LambdaQueryWrapper<Reward> wrapper = Wrappers.<Reward>lambdaQuery();
    //根据Id逆序排列
    wrapper.orderByDesc(Reward::getId);
    if (StrUtil.isNotBlank(search)) {
      switch (sort) {
        case "Itemname": //根据项目名称
          wrapper.like(Reward::getItemname, search);
          break;
        case "Rmember"://获奖人员
          wrapper.like(Reward::getRmember, search);
          break;
        case "Rewardname"://奖励名称
          wrapper.like(Reward::getRewardname, search);
          break;
        case "Conferdepartment"://授予单位
          wrapper.like(Reward::getConferdepartment, search);
          break;
        case "Rdate"://授予时间
          wrapper.like(Reward::getRdate, search);
          break;
      }
    }
    
    Page<Reward> AdminPage = rewardMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(AdminPage);
  }
  
  // 查询是否有这个主键
  @GetMapping("/itemname/{itemname}")
  public Result<?> getPname(@PathVariable String itemname) {
    Reward i = rewardMapper.selectOne(Wrappers.<Reward>lambdaQuery().eq(Reward::getItemname, itemname));
    if (i == null) {
      //表示不含有,则可以输入
      return Result.success();
    }
    //表示表中已存在,则不可以输入
    return Result.errorData("201", i);
  }
  
}

